{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0a46c20c",
   "metadata": {},
   "source": [
    "# NumPy Interfaces in MindSpore"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ac837cb6",
   "metadata": {},
   "source": [
    "[![](https://gitee.com/mindspore/docs/raw/master/resource/_static/logo_source.png)](https://gitee.com/mindspore/docs/blob/master/docs/programming_guide/source_en/numpy.ipynb)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "54e8412d",
   "metadata": {},
   "source": [
    "## Overview\n",
    "MindSpore Numpy package contains a set of Numpy-like interfaces, which allows developers to build models on MindSpore with similar syntax of Numpy."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1ac1b348",
   "metadata": {},
   "source": [
    "> This sample is suitable for CPU, GPU and Ascend environments."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c36fb23f",
   "metadata": {},
   "source": [
    "## Operator Functions\n",
    "\n",
    "Mindspore Numpy operators can be classified into four functional modules: `array generation`, `array operation`, `logic operation` and `math operation`. For details about the supported operators on the Ascend AI processors, GPU, and CPU, see [Numpy Interface List](https://www.mindspore.cn/doc/api_python/en/master/mindspore/mindspore.numpy.html).\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3e1665d1",
   "metadata": {},
   "source": [
    "### Array Generations\n",
    "\n",
    "Array generation operators are used to generate tensors.\n",
    "\n",
    "Here is an example to generate an array:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "37f588fc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "input_x = [1. 2. 3.]\n",
      "type of input_x = Tensor[Float32]\n"
     ]
    }
   ],
   "source": [
    "import mindspore.numpy as np\n",
    "import mindspore.ops as ops\n",
    "input_x = np.array([1, 2, 3], np.float32)\n",
    "print(\"input_x =\", input_x)\n",
    "print(\"type of input_x =\", ops.typeof(input_x))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3b74de41",
   "metadata": {},
   "source": [
    "Here we have more examples:\n",
    "\n",
    "#### Generate a tensor filled with the same element\n",
    "\n",
    "`np.full` can be used to generate a tensor with user-specified values:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "12c7c51b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[6. 6. 6.]\n",
      " [6. 6. 6.]]\n"
     ]
    }
   ],
   "source": [
    "input_x = np.full((2, 3), 6, np.float32)\n",
    "print(input_x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4d03732d",
   "metadata": {},
   "source": [
    "Here is another example to generate an array with the specified shape and filled with the value of 1:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "735ea8bf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1. 1. 1.]\n",
      " [1. 1. 1.]]\n"
     ]
    }
   ],
   "source": [
    "input_x = np.ones((2, 3), np.float32)\n",
    "print(input_x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a8ac3780",
   "metadata": {},
   "source": [
    "#### Generate tensors in a specified range\n",
    "\n",
    "Generate an arithmetic array within the specified range："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "7d52db50",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4]\n"
     ]
    }
   ],
   "source": [
    "input_x = np.arange(0, 5, 1)\n",
    "print(input_x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "96c61bb8",
   "metadata": {},
   "source": [
    "#### Generate tensors with specific requirement\n",
    "\n",
    "Generate a matrix where the lower elements are 1 and the upper elements are 0 on the given diagonal:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "1eadec6f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1. 1. 0.]\n",
      " [1. 1. 1.]\n",
      " [1. 1. 1.]]\n"
     ]
    }
   ],
   "source": [
    "input_x = np.tri(3, 3, 1)\n",
    "print(input_x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8c45c169",
   "metadata": {},
   "source": [
    "Another example, generate a 2-D matrix with a diagonal of 1 and other elements of 0:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "3f6ae5e1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1. 0.]\n",
      " [0. 1.]]\n"
     ]
    }
   ],
   "source": [
    "input_x = np.eye(2, 2)\n",
    "print(input_x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "478c6001",
   "metadata": {},
   "source": [
    "### Array Operations\n",
    "\n",
    "Array operations focus on tensor manipulation.\n",
    "\n",
    "#### Manipulate the shape of the tensor\n",
    "\n",
    "For example, transpose a matrix:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "68f72d5f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 2 4 6 8]\n",
      " [1 3 5 7 9]]\n"
     ]
    }
   ],
   "source": [
    "input_x = np.arange(10).reshape(5, 2)\n",
    "output = np.transpose(input_x)\n",
    "print(output)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ae8c3a96",
   "metadata": {},
   "source": [
    "Another example, swap two axes:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "c46d1cda",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(2, 1, 3)\n"
     ]
    }
   ],
   "source": [
    "input_x = np.ones((1, 2, 3))\n",
    "output = np.swapaxes(input_x, 0, 1)\n",
    "print(output.shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "69669608",
   "metadata": {},
   "source": [
    "#### Tensor splitting\n",
    "\n",
    "Divide the input tensor into multiple tensors equally, for example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "436a7010",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(Tensor(shape=[3], dtype=Int32, value= [0, 1, 2]), Tensor(shape=[3], dtype=Int32, value= [3, 4, 5]), Tensor(shape=[3], dtype=Int32, value= [6, 7, 8]))\n"
     ]
    }
   ],
   "source": [
    "input_x = np.arange(9)\n",
    "output = np.split(input_x, 3)\n",
    "print(output)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a9ca860f",
   "metadata": {},
   "source": [
    "#### Tensor combination\n",
    "\n",
    "Concatenate the two tensors according to the specified axis, for example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "2d0e9e58",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0  1  2  3  4 10 11 12 13 14]\n"
     ]
    }
   ],
   "source": [
    "input_x = np.arange(0, 5)\n",
    "input_y = np.arange(10, 15)\n",
    "output = np.concatenate((input_x, input_y), axis=0)\n",
    "print(output)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1c6c67d0",
   "metadata": {},
   "source": [
    "### Logic Operations\n",
    "\n",
    "Logic operations define computations related with boolean types.\n",
    "Examples of `equal` and `less` operations are as follows:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "26312e12",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "output of equal: [ True False False False False]\n",
      "output of less: [False  True  True  True  True]\n"
     ]
    }
   ],
   "source": [
    "input_x = np.arange(0, 5)\n",
    "input_y = np.arange(0, 10, 2)\n",
    "output = np.equal(input_x, input_y)\n",
    "print(\"output of equal:\", output)\n",
    "output = np.less(input_x, input_y)\n",
    "print(\"output of less:\", output)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6bd8b9ca",
   "metadata": {},
   "source": [
    "### Math Operations\n",
    "\n",
    "Math operations include basic and advanced math operations on tensors, and they have full support on Numpy broadcasting rules. Here are some examples:\n",
    "\n",
    "#### Sum two tensors\n",
    "\n",
    "The following code implements the operation of adding two tensors of `input_x` and `input_y`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "ae21f1af",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[4 6]\n",
      " [4 6]\n",
      " [4 6]]\n"
     ]
    }
   ],
   "source": [
    "input_x = np.full((3, 2), [1, 2])\n",
    "input_y = np.full((3, 2), [3, 4])\n",
    "output = np.add(input_x, input_y)\n",
    "print(output)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ed99cb92",
   "metadata": {},
   "source": [
    "#### Matrics multiplication\n",
    "\n",
    "The following code implements the operation of multiplying two matrices `input_x` and `input_y`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "af2790c2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[20. 23. 26. 29.]\n",
      " [56. 68. 80. 92.]]\n"
     ]
    }
   ],
   "source": [
    "input_x = np.arange(2*3).reshape(2, 3).astype('float32')\n",
    "input_y = np.arange(3*4).reshape(3, 4).astype('float32')\n",
    "output = np.matmul(input_x, input_y)\n",
    "print(output)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "224cb9cf",
   "metadata": {},
   "source": [
    "#### Take the average along a given axis\n",
    "\n",
    "The following code implements the operation of averaging all the elements of `input_x`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "78b73693",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.5\n"
     ]
    }
   ],
   "source": [
    "input_x = np.arange(6).astype('float32')\n",
    "output = np.mean(input_x)\n",
    "print(output)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "87711b9f",
   "metadata": {},
   "source": [
    "#### Exponential arithmetic\n",
    "\n",
    "The following code implements the operation of the natural constant `e` to the power of `input_x`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "9ac4add8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1.         2.7182817  7.389056  20.085537  54.59815  ]\n"
     ]
    }
   ],
   "source": [
    "input_x = np.arange(5).astype('float32')\n",
    "output = np.exp(input_x)\n",
    "print(output)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f2d456ac",
   "metadata": {},
   "source": [
    "## Interact With MindSpore Functions\n",
    "\n",
    "Since `mindspore.numpy` directly wraps MindSpore tensors and operators, it has all the advantages and properties of MindSpore. In this section, we will briefly introduce how to employ MindSpore execution management and automatic differentiation in `mindspore.numpy` coding scenarios. These include:\n",
    "\n",
    "- `ms_function`: for running codes in static graph mode for better efficiency.\n",
    "- `GradOperation`: for automatic gradient computation.\n",
    "- `mindspore.context`: for `mindspore.numpy` execution management.\n",
    "- `mindspore.nn.Cell`: for using `mindspore.numpy` interfaces in MindSpore Deep Learning Models.\n",
    "\n",
    "### Use ms_function to run code in static graph mode\n",
    "\n",
    "Let's first see an example consisted of matrix multiplication and bias add, which is a typical process in Neural Networks:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "1968a4e1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 768.  768.  768.  768.]\n",
      " [2816. 2816. 2816. 2816.]]\n"
     ]
    }
   ],
   "source": [
    "import mindspore.numpy as np\n",
    "\n",
    "x = np.arange(8).reshape(2, 4).astype('float32')\n",
    "w1 = np.ones((4, 8))\n",
    "b1 = np.zeros((8,))\n",
    "w2 = np.ones((8, 16))\n",
    "b2 = np.zeros((16,))\n",
    "w3 = np.ones((16, 4))\n",
    "b3 = np.zeros((4,))\n",
    "\n",
    "def forward(x, w1, b1, w2, b2, w3, b3):\n",
    "    x = np.dot(x, w1) + b1\n",
    "    x = np.dot(x, w2) + b2\n",
    "    x = np.dot(x, w3) + b3\n",
    "    return x\n",
    "\n",
    "print(forward(x, w1, b1, w2, b2, w3, b3))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4ebbc606",
   "metadata": {},
   "source": [
    "In this function, MindSpore dispatches each computing kernel to device separately. However, with the help of `ms_function`, we can compile all operations into a single static computing graph."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "0eb3b98d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 768.  768.  768.  768.]\n",
      " [2816. 2816. 2816. 2816.]]\n"
     ]
    }
   ],
   "source": [
    "from mindspore import ms_function\n",
    "\n",
    "forward_compiled = ms_function(forward)\n",
    "print(forward(x, w1, b1, w2, b2, w3, b3))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "66b52f69",
   "metadata": {},
   "source": [
    "> Currently, static graph cannot run in command line mode and not all python types can be passed into functions decorated with `ms_function`. For details about how to use `ms_function`, see [API: ms_function](https://www.mindspore.cn/doc/api_python/en/master/mindspore/mindspore.html#mindspore.ms_function)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "02c0a661",
   "metadata": {},
   "source": [
    "### Use GradOperation to compute deratives\n",
    "\n",
    "`GradOperation` can be used to take deratives from normal functions and functions decorated with `ms_function`. Take the previous example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "c3f36eb5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(Tensor(shape=[2, 4], dtype=Float32, value=\n",
       " [[ 5.12000000e+02,  5.12000000e+02,  5.12000000e+02,  5.12000000e+02],\n",
       "  [ 5.12000000e+02,  5.12000000e+02,  5.12000000e+02,  5.12000000e+02]]),\n",
       " Tensor(shape=[4, 8], dtype=Float32, value=\n",
       " [[ 2.56000000e+02,  2.56000000e+02,  2.56000000e+02 ...  2.56000000e+02,  2.56000000e+02,  2.56000000e+02],\n",
       "  [ 3.84000000e+02,  3.84000000e+02,  3.84000000e+02 ...  3.84000000e+02,  3.84000000e+02,  3.84000000e+02],\n",
       "  [ 5.12000000e+02,  5.12000000e+02,  5.12000000e+02 ...  5.12000000e+02,  5.12000000e+02,  5.12000000e+02]\n",
       "  [ 6.40000000e+02,  6.40000000e+02,  6.40000000e+02 ...  6.40000000e+02,  6.40000000e+02,  6.40000000e+02]]),\n",
       "  ...\n",
       " Tensor(shape=[4], dtype=Float32, value= [ 2.00000000e+00,  2.00000000e+00,  2.00000000e+00,  2.00000000e+00]))"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from mindspore import ops\n",
    "\n",
    "grad_all = ops.composite.GradOperation(get_all=True)\n",
    "grad_all(forward)(x, w1, b1, w2, b2, w3, b3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "377b4aa7",
   "metadata": {},
   "source": [
    "To take the gradient of `ms_function` compiled functions, first we need to set the execution mode to static graph mode."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "861b08c4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(Tensor(shape=[2, 4], dtype=Float32, value=\n",
       " [[ 5.12000000e+02,  5.12000000e+02,  5.12000000e+02,  5.12000000e+02],\n",
       "  [ 5.12000000e+02,  5.12000000e+02,  5.12000000e+02,  5.12000000e+02]]),\n",
       " Tensor(shape=[4, 8], dtype=Float32, value=\n",
       " [[ 2.56000000e+02,  2.56000000e+02,  2.56000000e+02 ...  2.56000000e+02,  2.56000000e+02,  2.56000000e+02],\n",
       "  [ 3.84000000e+02,  3.84000000e+02,  3.84000000e+02 ...  3.84000000e+02,  3.84000000e+02,  3.84000000e+02],\n",
       "  [ 5.12000000e+02,  5.12000000e+02,  5.12000000e+02 ...  5.12000000e+02,  5.12000000e+02,  5.12000000e+02]\n",
       "  [ 6.40000000e+02,  6.40000000e+02,  6.40000000e+02 ...  6.40000000e+02,  6.40000000e+02,  6.40000000e+02]]),\n",
       "  ...\n",
       " Tensor(shape=[4], dtype=Float32, value= [ 2.00000000e+00,  2.00000000e+00,  2.00000000e+00,  2.00000000e+00]))"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from mindspore import ms_function, context\n",
    "\n",
    "context.set_context(mode=context.GRAPH_MODE)\n",
    "grad_all = ops.composite.GradOperation(get_all=True)\n",
    "grad_all(ms_function(forward))(x, w1, b1, w2, b2, w3, b3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1b3efefa",
   "metadata": {},
   "source": [
    "For more details, see [API: GradOperation](https://www.mindspore.cn/doc/api_python/en/master/mindspore/ops/mindspore.ops.GradOperation.html)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "db492727",
   "metadata": {},
   "source": [
    "### Use mindspore.context to control execution mode\n",
    "\n",
    "Most functions in `mindspore.numpy` can run in Graph Mode and PyNative Mode, and can run on `CPU`，`GPU` and `Ascend`. Like MindSpore, users can manage the execution mode using `mindspore.context`："
   ]
  },
  {
   "cell_type": "markdown",
   "id": "24d87e38",
   "metadata": {},
   "source": [
    "```python\n",
    "from mindspore import context\n",
    "\n",
    "# Execucation in static graph mode\n",
    "context.set_context(mode=context.GRAPH_MODE)\n",
    "\n",
    "# Execucation in PyNative mode\n",
    "context.set_context(mode=context.PYNATIVE_MODE)\n",
    "\n",
    "# Execucation on CPU backend\n",
    "context.set_context(device_target=\"CPU\")\n",
    "\n",
    "# Execucation on GPU backend\n",
    "context.set_context(device_target=\"GPU\")\n",
    "\n",
    "# Execucation on Ascend backend\n",
    "context.set_context(device_target=\"Ascend\")\n",
    "...\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5c1e279f",
   "metadata": {},
   "source": [
    " For more details, see [API: mindspore.context](https://www.mindspore.cn/doc/api_python/en/master/mindspore/mindspore.context.html)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7968e4af",
   "metadata": {},
   "source": [
    "### Use mindspore.numpy in MindSpore Deep Learning Models\n",
    "\n",
    "`mindspore.numpy` interfaces can be used inside `nn.cell` blocks as well. For example, the above code can be modified to:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "dfae0641",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 768.  768.  768.  768.]\n",
      " [2816. 2816. 2816. 2816.]]\n"
     ]
    }
   ],
   "source": [
    "import mindspore.numpy as np\n",
    "from mindspore import context\n",
    "from mindspore.nn import Cell\n",
    "\n",
    "context.set_context(mode=context.GRAPH_MODE)\n",
    "\n",
    "x = np.arange(8).reshape(2, 4).astype('float32')\n",
    "w1 = np.ones((4, 8))\n",
    "b1 = np.zeros((8,))\n",
    "w2 = np.ones((8, 16))\n",
    "b2 = np.zeros((16,))\n",
    "w3 = np.ones((16, 4))\n",
    "b3 = np.zeros((4,))\n",
    "\n",
    "class NeuralNetwork(Cell):\n",
    "    def __init__(self):\n",
    "        super(NeuralNetwork, self).__init__()\n",
    "\n",
    "    def construct(self, x, w1, b1, w2, b2, w3, b3):\n",
    "        x = np.dot(x, w1) + b1\n",
    "        x = np.dot(x, w2) + b2\n",
    "        x = np.dot(x, w3) + b3\n",
    "        return x\n",
    "\n",
    "net = NeuralNetwork()\n",
    "\n",
    "print(net(x, w1, b1, w2, b2, w3, b3))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e2492f74",
   "metadata": {},
   "source": [
    "For more details on building Neural Network with MindSpore, see [MindSpore Training Guide](https://www.mindspore.cn/tutorial/training/en/master/index.html)."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "MindSpore-1.1.1",
   "language": "python",
   "name": "mindspore-1.1.1"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
